package com.gvitz.transaction.utils;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.gvitz.transaction.database.webserviceCards;
import com.gvitz.transaction.database.webserviceTransaction;
import com.gvitz.transaction.database.webserviceVendedores;


public class Config {

	public static final Boolean DEBUG = false;
	public static final String IP = "https://gvitz.com/";
	
	
	//tablas
	
	public static final String tabla_tarjetas= "tarjetas";	
	public static final String tabla_clientes= "clientes";	
	public static final String tabla_transacciones= "transacciones";
	
	
	// tabla transaccion
	
	public static final String trans_id= "_id";
	public static final String trans_monto= "monto";
	public static final String trans_id_cliente= "id_cliente";
	public static final String trans_nombre= "nombre_cliente";
	public static final String trans_fecha= "fecha";
	public static final String trans_id_tarjeta= "tarjeta";
	
	
	
	// tabla tarjeta
	
	public static final String card_id= "_id";
	public static final String card_nombre= "nombre";
	public static final String card_type= "tipo";
	public static final String card_number = "numero";
	
	
	// tabla cliente
	
	public static final String cliente_id= "_id";
	public static final String cliente_nombre= "nombre";
	public static final String cliente_direccion= "direccion";
	public static final String cliente_latitud= "latitud";
	public static final String cliente_longitud= "longitud";

	public static void trustEveryone() { 
        try { 
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 
        						@Override
						public boolean verify(String hostname,
								SSLSession session) {
							// TODO Auto-generated method stub
							return true;
						}}); 
                SSLContext context = SSLContext.getInstance("TLS"); 
                context.init(null, new X509TrustManager[]{new X509TrustManager(){ 
                        public void checkClientTrusted(X509Certificate[] chain, 
                                        String authType) throws CertificateException {} 
                        public void checkServerTrusted(X509Certificate[] chain, 
                                        String authType) throws CertificateException {} 
                        public X509Certificate[] getAcceptedIssuers() { 
                                return new X509Certificate[0]; 
                        }}}, new SecureRandom()); 
                HttpsURLConnection.setDefaultSSLSocketFactory( 
                                context.getSocketFactory()); 
        } catch (Exception e) { // should never happen 
                e.printStackTrace(); 
        } 
    }
		
	public static void updateNewRegister(Activity act, Context ctx){
		
		SharedPreferences prefs =  act.getSharedPreferences("preferencias", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        
		webserviceCards wc = new webserviceCards(ctx, act);
		webserviceVendedores wv = new webserviceVendedores(ctx, act);
		webserviceTransaction wt = new webserviceTransaction(ctx, act);
		
		wc.GetCards(prefs.getString("id_usuario",""));
		wt.GetTransaccions(prefs.getString("id_usuario",""));
		wv.getVendedores("'"+prefs.getString("fecha_actualizacion","")+"'");
		
		
 		//actualizar fecha de actualizacion
 		Date d = new Date();
 		SimpleDateFormat sdf= new SimpleDateFormat("yyyy'-'MM'-'dd HH:mm:ss");
 		editor.putString("fecha_actualizacion",sdf.format(d));				
 		editor.commit();
	}
		
	public static void updateInfoClientes(Activity act, Context ctx){
		
		SharedPreferences prefs =  act.getSharedPreferences("preferencias", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        
		webserviceVendedores wv = new webserviceVendedores(ctx, act);		
		wv.getVendedores("'"+prefs.getString("fecha_actualizacion","")+"'");
		
 		//actualizar fecha de actualizacion
 		Date d = new Date();
 		SimpleDateFormat sdf= new SimpleDateFormat("yyyy'-'MM'-'dd HH:mm:ss");
 		editor.putString("fecha_actualizacion",sdf.format(d));				
 		editor.commit();
	}
	
    
}

